package com.salinity.kun.task;

import java.io.IOException;
import java.nio.file.Path;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Map;

import com.salinity.kun.helper.CalculationHelper;
import com.salinity.kun.helper.ConstantHelper;
import com.salinity.kun.model.FourierSeriesInput;
import com.salinity.kun.model.HydrologicalDataList;
import com.salinity.kun.model.HydrologicalDataList.DataItem;

/**
 *
 * @author CK
 *
 */
public class BatchFSTask implements ITask {

	
	@Override
	public boolean run() throws IOException, ParseException {
		//取数据
		SimpleDateFormat sdf = ConstantHelper.getDateFormat();
		String startDateStr = ConstantHelper.getStartDate();
		String endDateStr = ConstantHelper.getEndDate();
		double T = ConstantHelper.getT0();
		double deltaT = ConstantHelper.getDT();
		int seriesCount = ConstantHelper.getSeriesCount();
		
		String[] StationNames = ConstantHelper.getStationNameArray();

		//运算，并获取结果
		Double[] f;
		String[] outputItems = ConstantHelper.getOutputItems();
		FourierSeriesInput fourierSeriesInput = new FourierSeriesInput(T,deltaT,seriesCount);
		Path outputPath;
		Map<DataItem,String> namesMap = CalculationHelper.getNamesMap();
		String ftName="";
		for(String stationName:StationNames) {

			HydrologicalDataList hydrologicalDataList = CalculationHelper.
					getDataByDateFromFile(sdf.parse(startDateStr), sdf.parse(endDateStr), stationName);
			
			for(DataItem dataItem:DataItem.values()) {
				//Exclude item DATE
				if(dataItem.equals(DataItem.DATE)) continue;
				//get values of this item as Double Array
				f = (Double[]) hydrologicalDataList.getItem(dataItem);
				//set to fourierSeriesInput 
				fourierSeriesInput.setF(f);
				//get name
				String tempName = namesMap.get(dataItem);
				ftName = tempName==null?"":tempName;
				fourierSeriesInput.setName(ftName);
				//create output path
			    outputPath = CalculationHelper.getResultPath(stationName, ftName+"@"+Math.round(T));
			    //write to file
				CalculationHelper.writeToFile(fourierSeriesInput, outputItems, outputPath);			
			}			
		}	
		return true;
	}
	
	public static void main(String[] args) {
		ITask batchTask = new BatchFSTask();
		try {
			batchTask.run();
		} catch (IOException e) {
			 
			e.printStackTrace();
		} catch (ParseException e) {
			 
			e.printStackTrace();
		}
	}

}
